import { Controller } from 'egg';

import { Result } from '../util';
import { uuidCommonRule, pageSizeCommonRule } from './validate-rule';

class TemplateController extends Controller {
  /**
   * 查找所有模板,分页展示
   */
  async show() {
    const { ctx } = this;

    ctx.validate({
      title: {
        required: false,
        type: 'string',
      },
      page: {
        type: 'number',
      },
      pageSize: pageSizeCommonRule
    }, ctx.query);

    const { title, page, pageSize } = ctx.query;

    const templateList = await this.ctx.service.template.findTemplate(
      title,
      Number(page),
      Number(pageSize)
    );

    ctx.body = new Result({ templateList });
  }

  async create() {
    const { ctx } = this;

    ctx.validate({
      title: {
        type: 'string',
        length: 100
      },
      description: {
        required: false,
        type: 'string',
      },
      epilog: {
        required: false,
        type: 'string',
      },
      prefixion: {
        required: false,
        type: 'string',
        length: 100,
      },
      logo: {
        type: 'string',
        max: 255,
      }
    }, ctx.request.body);

    const {
      title, description, epilog, prefixion, logo
    } = ctx.request.body;

    const template = await this.ctx.service.template.createTemplate(
      title, description, epilog, prefixion, logo
    );

    if (!template) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '问卷名重复！');
      return;
    }

    ctx.body = new Result({ template }, '创建问卷成功！');
  }

  async update() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule,
      title: {
        type: 'string',
        length: 100
      },
      description: {
        required: false,
        type: 'string',
      },
      epilog: {
        required: false,
        type: 'string',
      },
      prefixion: {
        required: false,
        type: 'string',
        length: 100,
      },
      logo: {
        type: 'string',
        max: 255,
      }
    }, ctx.request.body);

    const {
      uuid, title, description, epilog, prefixion, logo
    } = ctx.request.body;

    const template = await this.ctx.service.template.updateTemplate(
      uuid, title, description, epilog, prefixion, logo
    );

    ctx.body = new Result({ template }, '更新问卷成功！');
  }

  async delete() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.request.body);

    const { uuid } = ctx.request.body;

    const result = await ctx.service.template.findQuestionnaireByUuid(uuid);

    if (!result) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '删除失败，请先删除该问卷下的二级问卷！');
      return;
    }

    const template = await ctx.service.template.deleteTemplate(uuid);

    ctx.body = new Result({ template }, '删除问卷成功！');
  }

  async find() {
    const { ctx } = this;

    ctx.validate({
      uuid: uuidCommonRule
    }, ctx.query);

    const { uuid } = ctx.query;

    const template = await this.ctx.service.template.findOneTemplate(uuid);

    if (template === null) {
      ctx.body = new Result(undefined, '该问卷不存在');
      return;
    }

    ctx.body = new Result({ template });
  }

  /**
   * 作为选项吧被查询，会按照首个文字拼音分组
   */
  async optionList() {
    const { ctx } = this;

    ctx.validate({
      title: {
        required: false,
        type: 'string',
        max: 100,
      },
      isAttachAll: {
        required: false,
        type: 'boolean',
      }
    }, ctx.query);

    const { title, isAttachAll } = ctx.query;

    const templateOptionList = await ctx
      .service
      .template
      .showOptionList(title, Boolean(isAttachAll));

    ctx.body = new Result(templateOptionList);
  }
}

export default TemplateController;
